package com.xuxin.pda.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.xuxin.pda.R;
import com.xuxin.pda.adapter.TrashSummaryAdapter;
import com.xuxin.pda.base.BaseActivity;
import com.xuxin.pda.bean.TrashSummaryBean;
import com.xuxin.pda.contract.TrashSummaryContract;
import com.xuxin.pda.presenter.TrashSummaryPresenter;
import com.xuxin.pda.utils.DatePickerUtils;
import com.xuxin.pda.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2021/2/7 0007
 * 医废日报表
 */
public class TrashDayActivity extends BaseActivity implements TrashSummaryContract.View {
    private TrashSummaryContract.Presenter presenter = new TrashSummaryPresenter(this);
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;
    @BindView(R.id.trash_day_date)
    TextView trash_day_date;

    private TrashSummaryAdapter mTrashSummaryAdapter;
    private String time_year;
    private String time_month;
    private String time_day;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_trash_summary);
    }

    @Override
    public void initView() {
        title_name.setText("明慈医废收集详情");
        title_right.setVisibility(View.INVISIBLE);

        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setEnableRefresh(false);
        mTrashSummaryAdapter = new TrashSummaryAdapter();
        RecycleUtils.init(smart_recycle);
        smart_recycle.setAdapter(mTrashSummaryAdapter);
    }

    @Override
    public void initData() {
        time_year = TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy"));
        time_month = TimeUtils.getNowString(TimeUtils.getSafeDateFormat("MM"));
        time_day = TimeUtils.getNowString(TimeUtils.getSafeDateFormat("dd"));
        trash_day_date.setText(time_year + "-" + time_month + "-" + time_day + "日数据统计");
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.trash_day_date})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.trash_day_date) {
            DatePickerUtils.showDate(context, new DatePickerUtils.onListener2() {
                @Override
                public void onShow(String time, String year, String month, String day) {
                    trash_day_date.setText(year + "-" + month + "-" + day + "日数据统计");
                    time_year = year;
                    time_month = month;
                    time_day = day;
                    popup.setTitle("查询中...").show();
                    handler.sendEmptyMessageDelayed(0, 500);
                }
            });
        }
    }

    @Override
    public void onSuccess(TrashSummaryBean trashSummaryBean) {
        if (null != trashSummaryBean && null != trashSummaryBean.getResult() && null != trashSummaryBean.getResult().getM_In_MOrD_Query()) {
            mTrashSummaryAdapter.setList(trashSummaryBean.getResult().getM_In_MOrD_Query());
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getTrashSummary(time_year, time_month, time_day, "In_D");
            }
            return false;
        }
    });
}